package org.wk.common.qiniu;

import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;


public class BucketManagerFacade {

    public static void main(String[] args){
        try {
            String[] buckets = BucketManagerFacade.buckets();
            for (String bucket : buckets){
                System.out.println(bucket);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 列举一个账号的所有空间
     * @return
     * @throws Exception
     */
    public static String[] buckets() throws Exception {
        BucketManager bucketManager = ClientBuilder.createBucketManager();
        return bucketManager.buckets();
    }


    /**
     * 资源复制
     *
     * 注意：
     *      源空间和目标空间必须属于相同帐号，跨帐号移动文件不支持。
     *      资源不支持跨存储区域copy。
     * @param fromBucket
     * @param fromFileKey
     * @param toBucket
     * @param toFileKey
     * @throws Exception
     */
    public static Response copy(String fromBucket, String fromFileKey, String toBucket, String toFileKey) throws Exception {
        BucketManager bucketManager = ClientBuilder.createBucketManager();
        return bucketManager.copy(fromBucket, fromFileKey, toBucket, toFileKey, false);
    }

    /**
     * 资源移动
     *
     * 注意：
     *      源空间和目标空间必须属于相同帐号，跨帐号移动文件不支持。
     *      资源不支持跨存储区域move。
     * @param fromBucket
     * @param fromFileKey
     * @param toBucket
     * @param toFileKey
     * @throws Exception
     */
    public static Response move(String fromBucket, String fromFileKey, String toBucket, String toFileKey) throws Exception {
        BucketManager bucketManager = ClientBuilder.createBucketManager();
        return bucketManager.move(fromBucket, fromFileKey, toBucket, toFileKey);
    }

    /**
     * 资源重命名
     * @param bucket
     * @param oldFileKey
     * @param newFileKey
     * @return
     * @throws Exception
     */
    public static Response rename(String bucket, String oldFileKey, String newFileKey) throws Exception {
        BucketManager bucketManager = ClientBuilder.createBucketManager();
        return bucketManager.rename(bucket, oldFileKey, newFileKey);
    }

    /**
     * 资源删除
     * @param bucket
     * @param key
     * @return
     * @throws Exception
     */
    public static Response delete(String bucket, String key) throws Exception {
        BucketManager bucketManager = ClientBuilder.createBucketManager();
        return bucketManager.delete(bucket, key);
    }

}
